package com.nasarius.bank.scorecard.web.controller.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.nasarius.bank.scorecard.dao.BankDetailsDAO;
import com.nasarius.bank.scorecard.dao.FactorsDAO;
import com.nasarius.bank.scorecard.dao.SurveyDetailsDAO;
import com.nasarius.bank.scorecard.dao.SurveyQuestionsDAO;
import com.nasarius.bank.scorecard.dao.TopicsDAO;
import com.nasarius.bank.scorecard.domain.Factors;
import com.nasarius.bank.scorecard.domain.SurveyDetails;
import com.nasarius.bank.scorecard.domain.SurveyQuestions;
import com.nasarius.bank.vo.SurveyDetailsVO;

@Controller
public class SurveyDetailsController {
	protected final Log LOG = LogFactory.getLog(getClass());
	@Autowired
	private SurveyDetailsDAO surveyDetailsDAO;
	@Autowired
	private SurveyQuestionsDAO surveyQuestionsDAO;	
	@Autowired		
	private BankDetailsDAO bankDetailsDAO;	
	@Autowired
	private FactorsDAO factorsDAO;
	@Autowired
	private TopicsDAO topicsDAO;
	@InitBinder
	protected void initBinder(ServletRequestDataBinder binder) {
			binder.registerCustomEditor(List.class,new CustomCollectionEditor(List.class));
			binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
	}
	
	@ModelAttribute
	public SurveyDetailsVO newRequest(@RequestParam(required = false) Integer surveyId,@RequestParam(required = false) Integer questionId, Model model) {
		SurveyDetailsVO surveyDetailsVO= new SurveyDetailsVO();
		List<SurveyDetails> allSurveyDetails =null;
		surveyDetailsVO.setBankDetailsList(bankDetailsDAO.selectAllActiveBankDetails());	
		List<Factors> allFactors = factorsDAO.selectAllFactors();
		surveyDetailsVO.setTopicsList(topicsDAO.selectAllTopics());
		List<Factors> allQualitativeFactors = new ArrayList<Factors>();
		for(Factors factor: allFactors){
			if (factor != null && factor.getFactortype() != null && factor.getFactortype().equalsIgnoreCase("Qualitative")){
				allQualitativeFactors.add(factor);
			}
		}
		surveyDetailsVO.setFactorsList(allQualitativeFactors);
		if(surveyId==null && questionId==null){			
			allSurveyDetails = surveyDetailsDAO.selectAllSurveyDetails();
			model.addAttribute("allSurveyDetails", allSurveyDetails);
			if(surveyDetailsVO.getSelectedSurvey() == null){
				surveyDetailsVO.setSelectedSurvey(new SurveyDetails());
			}
			surveyDetailsVO.getSelectedSurvey().setCompleteindicator("N");			
		}		
		if(surveyId!=null){
			surveyDetailsVO.setSelectedSurvey(surveyDetailsDAO.selectSurveyDetailsByPrimaryKey(surveyId));
			if(surveyDetailsVO.getSelectedSurvey().getCompleteindicator() == null){
				surveyDetailsVO.getSelectedSurvey().setCompleteindicator("N");
			}
			surveyDetailsVO.setSurveyQuestions(surveyQuestionsDAO.selectSurveyQuestionsBySurveyID(surveyId));
			if(questionId !=null){
				surveyDetailsVO.setSelectedQuestion(surveyQuestionsDAO.selectSurveyQuestionsByPrimaryKey(questionId));
			}
		}		
		return surveyDetailsVO;
	}
    @RequestMapping(value="/surveydetails/update", method=RequestMethod.POST)
    public String update(@RequestParam(required = false) Integer surveyId,@RequestParam(required = false) Integer questionId, SurveyDetailsVO surveyDetailsVO, Model model) {
    	Integer surveyID=null;
    	boolean isSurveyExist = false;
    	List<SurveyDetails> surveyList = surveyDetailsDAO.selectSurveyByName(surveyDetailsVO.getSelectedSurvey().getSurveyname());
		if(surveyDetailsVO.getSelectedSurvey().getSurveyid()==null){
			System.out.println("balaji survey copy"+surveyDetailsVO.toString());
			
			
		//	List<SurveyDetails> surveyList = surveyDetailsDAO.selectSurveyByName(surveyDetailsVO.getSelectedSurvey().getSurveyname());
			
			if(surveyList == null || (surveyList != null && surveyList.size() == 0)){
				surveyID = surveyDetailsDAO.insertSurveyDetails(surveyDetailsVO.getSelectedSurvey());
			}else{
				isSurveyExist = true;
				model.addAttribute("surveyExist","Y");
			}
		}else if(surveyDetailsVO.getSelectedSurvey().getSurveyid()!= null && (surveyList == null || (surveyList != null && surveyList.size() == 0))){
			
			
			
				surveyDetailsVO.getSelectedSurvey().setSurveyid(null);
				surveyDetailsVO.getSelectedSurvey().setCompleteindicator("N");
				surveyID = surveyDetailsDAO.insertSurveyDetails(surveyDetailsVO.getSelectedSurvey());
				for (SurveyQuestions surveyQuestions : surveyDetailsVO.getSurveyQuestions())
				{
					surveyQuestions.setSurveyid(surveyID);
					surveyQuestions.setSurveyquestionid(null);
					surveyQuestionsDAO.insertSurveyQuestions(surveyQuestions);
				}
					
			
		}
		else{
			surveyDetailsDAO.updateSurveyDetailsByPrimaryKey(surveyDetailsVO.getSelectedSurvey());
			surveyDetailsVO.getSelectedQuestion().setSurveyid(surveyId);
		}
		if(!isSurveyExist){
			if(StringUtils.isNotBlank(surveyDetailsVO.getSelectedQuestion().getQuestiondescription()) && 
					surveyQuestionsDAO.selectSurveyQuestionsByPrimaryKey(surveyDetailsVO.getSelectedQuestion().getSurveyquestionid())==null){
				if(surveyDetailsVO.getSelectedQuestion().getSurveyid() == null){
					surveyDetailsVO.getSelectedQuestion().setSurveyid(surveyID);
				}
				surveyQuestionsDAO.insertSurveyQuestions(surveyDetailsVO.getSelectedQuestion());
			}
			else{
				surveyQuestionsDAO.updateSurveyQuestionsByPrimaryKey(surveyDetailsVO.getSelectedQuestion());
			}
			if(surveyId==null){
				return "redirect:/surveydetails/edit.html?surveyId="+surveyID; 
			}else{
				return "redirect:/surveydetails/edit.html?surveyId="+surveyId; 
			}
		}
		return null;
    }
    @RequestMapping(value="/surveydetails/deletequestion", method=RequestMethod.GET)
    public String deleteQuestion(@RequestParam(required = false) Integer surveyId,@RequestParam(required = false) Integer questionId, SurveyDetailsVO surveyDetailsVO, Model model) {
    	surveyQuestionsDAO.deleteSurveyQuestionsByPrimaryKey(questionId);
		return "redirect:/surveydetails/edit.html?surveyId="+surveyId; 
    }  
    @RequestMapping(value="/surveydetails/deletesurvey", method=RequestMethod.GET)
    public String deleteSurvey(@RequestParam(required = false) Integer surveyId, SurveyDetailsVO surveyDetailsVO, Model model) {
    	surveyQuestionsDAO.deleteSurveyQuestionsBySurveyId(surveyId);
    	surveyDetailsDAO.deleteSurveyDetailsByPrimaryKey(surveyId);
		return "redirect:/surveydetails/view.html"; 
    }    
    @RequestMapping(value="/surveydetails/completeSurvey", method=RequestMethod.POST)
    public String completeSurvey(@RequestParam(required = false) Integer surveyId, SurveyDetailsVO surveyDetailsVO, Model model) {
		surveyDetailsDAO.updateSurveyDetailsCompleteIndicator(surveyId);
		return "redirect:/surveydetails/edit.html?surveyId="+surveyId; 
    }     
	/**
     * <p>Person form request.</p>
     * 
     * <p>Expected HTTP GET and request '/surveydetails/view'.</p>
     */
    @RequestMapping(value="/surveydetails/view", method=RequestMethod.GET)
    public void form() {}
    
    @RequestMapping(value="/surveydetails/edit", method=RequestMethod.GET)
    public void edit() {}
    
	public void setSurveyDetailsDAO(SurveyDetailsDAO surveyDetailsDAO) {
		this.surveyDetailsDAO = surveyDetailsDAO;
	} 
	public void setSurveyQuestionsDAO(SurveyQuestionsDAO surveyQuestionsDAO) {
		this.surveyQuestionsDAO = surveyQuestionsDAO;
	}
	public void setBankDetailsDAO(BankDetailsDAO bankDetailsDAO) {
		this.bankDetailsDAO = bankDetailsDAO;
	}
	public void setFactorsDAO(FactorsDAO factorsDAO) {
		this.factorsDAO = factorsDAO;
	}

	public void setTopicsDAO(TopicsDAO topicsDAO) {
		this.topicsDAO = topicsDAO;
	}
}
